﻿/*
Copyright (c) 2011 <a href="http://www.gutgames.com">James Craig</a>

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.*/

#region Usings
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using HaterAide.Interfaces;
using HaterAide.SQL.DataClasses.Objects;

#endregion

namespace HaterAide.SQL.SQLServer.Statements
{
    /// <summary>
    /// Deletes an object
    /// </summary>
    internal static class Delete
    {
        #region Internal Static Functions

        internal static System.Collections.Generic.IEnumerable<DataClasses.Objects.Command> GetCommands(object Object, IClass ClassDefinition, IAttributeMap ClassMapping, IClassManager ClassManager, IMappingManager MappingManager)
        {
            List<Command> Commands = new List<Command>();
            if (Object == null)
                return Commands;
            SetupLists(Object, ClassMapping);
            Commands.AddRange(List.GetDeleteListsCommands(Object, Object.GetType(), ClassMapping, ClassDefinition));
            Commands.AddRange(GetCascadeDeleteCommands(Object, ClassDefinition, ClassMapping, ClassManager, MappingManager));
            object IDObject = Utilities.Reflection.Reflection.GetPropertyValue(Object, ClassMapping.IDProperties[0].Name);
            string IDPropertyName = ((HaterAide.SQL.DataClasses.Column)ClassMapping.IDProperties[0].AssociatedTable).Name;
            SqlDbType IDDataType = ((HaterAide.SQL.DataClasses.Column)ClassMapping.IDProperties[0].AssociatedTable).DataType;
            Command TempCommand = new Command();
            string StoredProcedure = "";
            if (ClassMapping.AssociatedTable.Name.EndsWith("_"))
            {
                StoredProcedure = ClassMapping.AssociatedTable.Name + "Delete";
            }
            else
            {
                StoredProcedure = ClassMapping.AssociatedTable.Name + "_Delete";
            }
            TempCommand.StoredProcedure = StoredProcedure;
            TempCommand.CommandType = Enums.CommandType.NonQuery;
            TempCommand.Variables.Add(new Variable("@" + IDPropertyName, IDObject, IDDataType));
            Commands.Add(TempCommand);
            return Commands;
        }

        #endregion

        #region Private Static Functions

        private static void SetupLists(object Object, IAttributeMap ClassMapping)
        {
            foreach (HaterAide.Mappings.Attribute Property in ClassMapping.Properties)
            {
                object Value = Utilities.Reflection.Reflection.GetPropertyValue(Object, Property.Name);
            }
        }

        private static IEnumerable<Command> GetCascadeDeleteCommands(object Object, IClass ClassDefinition, IAttributeMap ClassMapping, IClassManager ClassManager, IMappingManager MappingManager)
        {
            List<Command> Commands = new List<Command>();
            foreach (HaterAide.Mappings.Attribute Property in ClassMapping.Properties)
            {
                if (Property.Cascade)
                {
                    object Value = Utilities.Reflection.Reflection.GetPropertyValue(Object, Property.Name);
                    if (Value != null)
                    {
                        if (Value is IEnumerable)
                        {
                            IEnumerable TempValue = (IEnumerable)Value;
                            foreach (object MappedClassObject in TempValue)
                            {
                                Commands.AddRange(SQLBuilder.GetDeleteCommands(MappedClassObject, ClassManager, MappingManager));
                            }
                        }
                        else
                        {
                            Commands.AddRange(SQLBuilder.GetDeleteCommands(Value, ClassManager, MappingManager));
                        }
                    }
                }
            }
            return Commands;
        }

        #endregion
    }
}